package com.yzt.p_base.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.text.TextUtils;

import com.yzt.p_base.AppUtils;

/**
 * author : bob
 * date   : 2022/11/29
 * desc   :
 */
public class NetworkUtils {

    public static final String NETWORK_PROXY_PAGE = "http://api.longyouquwa.cn/html/network/index.html";

    /**
     * 是否有网
     */
    public static boolean isNetAvailable() {
        try {
            ConnectivityManager cm = (ConnectivityManager) AppUtils.getApp().getSystemService(Context.CONNECTIVITY_SERVICE);
            if (isWifiAvailable()) {
                return true;
            }
            if (cm == null) {
                return false;
            }
            // 取当前默认的连接信息
            NetworkInfo netinfo = cm.getActiveNetworkInfo();
            if (netinfo != null && netinfo.isConnected()) {
                return true;
            }
            NetworkInfo.State mobile = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
            return mobile == NetworkInfo.State.CONNECTED;
        } catch (Exception e) {
            e.printStackTrace();
            return true;
        }
    }

    /**
     * 是否是wifi
     */
    public static boolean isWifiAvailable() {
        WifiManager wifi = (WifiManager) AppUtils.getApp().getSystemService(Context.WIFI_SERVICE);
        return wifi.isWifiEnabled();
    }

    /**
     * 判断网络是否连接
     * <p>需添加权限 {@code <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>}</p>
     *
     * @return {@code true}: 是<br>{@code false}: 否
     */
    public static boolean isConnected() {
        NetworkInfo info = getActiveNetworkInfo();
        return info != null && info.isConnected();
    }

    /**
     * 获取活动网络信息
     * <p>需添加权限 {@code <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>}</p>
     *
     * @return NetworkInfo
     */
    private static NetworkInfo getActiveNetworkInfo() {
        return ((ConnectivityManager) AppUtils.getApp().getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
    }

    /**
     * wifi是否存在代理
     */
    public static boolean isWifiProxy() {

        if(!isWifiAvailable()) {
            return false;
        }

        Context context = AppUtils.getApp();
        final boolean IS_ICS_OR_LATER = Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH;

        String proxyAddress;

        int proxyPort;

        if (IS_ICS_OR_LATER) {

            proxyAddress = System.getProperty("http.proxyHost");

            String portStr = System.getProperty("http.proxyPort");

            proxyPort = Integer.parseInt((portStr != null ? portStr : "-1"));

        } else {

            proxyAddress = android.net.Proxy.getHost(context);

            proxyPort = android.net.Proxy.getPort(context);

        }

        return (!TextUtils.isEmpty(proxyAddress)) && (proxyPort != -1);
    }
}
